#!/bin/bash
#
# Linux/macOS 代理服务器配置脚本
#

PROXY_SERVER="192.168.31.7:17897"
BACKUP_PROXY="127.0.0.1:7890"

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# 测试代理连接
test_proxy_connection() {
    local proxy=$1
    echo -e "${CYAN}测试代理连接: $proxy${NC}"
    
    if curl --connect-timeout 10 --max-time 20 --proxy "http://$proxy" -s "http://www.google.com" > /dev/null 2>&1; then
        echo -e "${GREEN}代理可用: $proxy${NC}"
        return 0
    else
        echo -e "${RED}代理不可用: $proxy${NC}"
        return 1
    fi
}

# 配置系统代理
set_system_proxy() {
    local proxy=$1
    echo -e "${CYAN}配置系统代理...${NC}"
    
    # 设置环境变量
    export HTTP_PROXY="http://$proxy"
    export HTTPS_PROXY="http://$proxy"
    export ALL_PROXY="http://$proxy"
    
    # 添加到 shell 配置文件
    SHELL_CONFIGS=(
        "$HOME/.bashrc"
        "$HOME/.zshrc"
        "$HOME/.profile"
    )
    
    for config in "${SHELL_CONFIGS[@]}"; do
        if [ -f "$config" ]; then
            if ! grep -q "HTTP_PROXY" "$config"; then
                echo "" >> "$config"
                echo "# Proxy settings" >> "$config"
                echo "export HTTP_PROXY=http://$proxy" >> "$config"
                echo "export HTTPS_PROXY=http://$proxy" >> "$config"
                echo "export ALL_PROXY=http://$proxy" >> "$config"
                echo -e "${GREEN}已添加到 $config${NC}"
            fi
        fi
    done
    
    echo -e "${GREEN}系统代理配置完成${NC}"
}

# 配置 Git 代理
set_git_proxy() {
    local proxy=$1
    echo -e "${CYAN}配置 Git 代理...${NC}"
    
    git config --global http.proxy "http://$proxy"
    git config --global https.proxy "http://$proxy"
    git config --global http.https://github.com.proxy "http://$proxy"
    
    echo -e "${GREEN}Git 代理配置完成${NC}"
}

# 配置 npm 代理
set_npm_proxy() {
    local proxy=$1
    echo -e "${CYAN}配置 npm 代理...${NC}"
    
    npm config set proxy "http://$proxy"
    npm config set https-proxy "http://$proxy"
    npm config set registry "https://registry.npmmirror.com/"
    
    echo -e "${GREEN}npm 代理配置完成${NC}"
}

# 配置 pip 代理
set_pip_proxy() {
    local proxy=$1
    echo -e "${CYAN}配置 pip 代理...${NC}"
    
    PIP_CONFIG_DIR="$HOME/.pip"
    PIP_CONFIG_FILE="$PIP_CONFIG_DIR/pip.conf"
    
    mkdir -p "$PIP_CONFIG_DIR"
    
    cat > "$PIP_CONFIG_FILE" << EOF
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
trusted-host = pypi.tuna.tsinghua.edu.cn
proxy = http://$proxy
EOF
    
    echo -e "${GREEN}pip 代理配置完成${NC}"
}

# 配置 Cargo 代理
set_cargo_proxy() {
    local proxy=$1
    echo -e "${CYAN}配置 Cargo 代理...${NC}"
    
    CARGO_CONFIG_DIR="$HOME/.cargo"
    CARGO_CONFIG_FILE="$CARGO_CONFIG_DIR/config"
    
    mkdir -p "$CARGO_CONFIG_DIR"
    
    cat > "$CARGO_CONFIG_FILE" << EOF
[registry]
default = "rustcc"

[source.rustcc]
registry = "https://code.rustcc.com.cn/crates.io-index"

[net]
git-fetch-with-cli = true

[http]
proxy = "http://$proxy"
check-revoke = false

[https]
proxy = "http://$proxy"
EOF
    
    echo -e "${GREEN}Cargo 代理配置完成${NC}"
}

# 配置 APT 代理 (Ubuntu/Debian)
set_apt_proxy() {
    local proxy=$1
    echo -e "${CYAN}配置 APT 代理...${NC}"
    
    if command -v apt-get > /dev/null; then
        sudo tee /etc/apt/apt.conf.d/95proxies > /dev/null << EOF
Acquire::http::Proxy "http://$proxy";
Acquire::https::Proxy "http://$proxy";
EOF
        echo -e "${GREEN}APT 代理配置完成${NC}"
    else
        echo -e "${YELLOW}跳过 APT 配置 (非 Debian 系系统)${NC}"
    fi
}

# 主程序
main() {
    echo -e "${CYAN}=== 代理服务器配置工具 ===${NC}"
    echo -e "${YELLOW}主要代理: $PROXY_SERVER${NC}"
    echo -e "${YELLOW}备用代理: $BACKUP_PROXY${NC}"
    
    # 测试代理连接
    if test_proxy_connection "$PROXY_SERVER"; then
        ACTIVE_PROXY="$PROXY_SERVER"
    elif test_proxy_connection "$BACKUP_PROXY"; then
        ACTIVE_PROXY="$BACKUP_PROXY"
    else
        echo -e "${RED}所有代理均不可用，退出配置${NC}"
        exit 1
    fi
    
    echo -e "${GREEN}使用代理: $ACTIVE_PROXY${NC}"
    
    # 配置各项代理
    set_system_proxy "$ACTIVE_PROXY"
    set_git_proxy "$ACTIVE_PROXY"
    set_npm_proxy "$ACTIVE_PROXY"
    set_pip_proxy "$ACTIVE_PROXY"
    set_cargo_proxy "$ACTIVE_PROXY"
    set_apt_proxy "$ACTIVE_PROXY"
    
    echo -e "${CYAN}=== 代理配置完成 ===${NC}"
    echo -e "${GREEN}当前使用代理: $ACTIVE_PROXY${NC}"
    echo -e "${YELLOW}请重新加载 shell 配置: source ~/.bashrc 或重新启动终端${NC}"
}

# 执行主程序
main "$@"